<template>
  <div>
    <BaseBillButtonGroup @add="addBill" @save="saveBill" @gotoList="gotoList" />

    <!-- <BaseBillTopInfo :billNo="master.billNo"
      :billStatus="master.billStatus"
      :createUserName="master.createUserId"
      :createTime="master.createTime"
      auditTime=""
      auditUserName="" /> -->

    <BaseBillTopInfo v-if="isSave" billNo="20250225001" billStatus="已审核" createUserName="管理员"
      createTime="2025-02-25 17:00" auditTime="2025-02-25 18:00" auditUserName="管理员" remark="123" />

    <BaseBillTopInfo v-else billNo="" billStatus="" createUserName="" createTime="" auditTime="" auditUserName=""
      remark="" />

    <MkCardForm :model="master" ref="formRef" :column-count="4" formId="basInvMessage">
      <MkInput label="出库单编号" v-model.trim="master.bloodBank" :isEdit="!isSave" @keyup-enter="keyupEnter"
        :config="{ isEdit: !isSave, required: false }" />
      <MkSelect label="血液来源" prop="bloodStation" v-model="master.bloodStation" :isEdit="!isSave"
        :options="bloodStationOptions" :config="{ isEdit: !isSave, required: false }" />
      <MkSelect label="入库院区" prop="courtArea" v-model="master.courtArea" :isEdit="!isSave" :options="courtAreaOptions"
        :config="{ isEdit: !isSave, required: false }" />
      <MkInput label="备注" v-model="master.remark" :isEdit="!isSave" :config="{ isEdit: !isSave, required: false }" />
    </MkCardForm>
    <div class="resize-container">
      <MkCard class="component-left">
        <div v-if="!isSave">
          <el-checkbox :disabled="isSave" v-model="isScan">启用扫码</el-checkbox>
          <div class="scanBox" v-if="isScan">
            <div class="scanInpBox">
              <el-input v-model.trim="scanBarCode" placeholder="请扫码血袋号..." @keyup.enter="scanCode">
                <template #prefix>
                  <!-- <<SvgIcon icon-class="scancode"
                  style="color: #000" />> -->
                  <el-icon><ele-FullScreen /></el-icon>
                </template>
              </el-input>
            </div>
            <div class="buttonBox">
              <div class="delBtn">
                <el-button icon="ele-Delete" @click="delInfo">删除</el-button>
              </div>
            </div>
          </div>
        </div>
        <MkTable :data="bloodList">
          <MkColumnIndex />
          <MkColumnXueYe />
          <MkColumn label="采血日期" minWidth="120px" prop="makeDate" />
          <MkColumn label="金额" min-width="80px" prop="price" />
        </MkTable>
      </MkCard>
      <div class="dividebox" ref="divideRef" v-resize="['.component-left', '.component-right', 850]"></div>
      <InvBloodSummary :bloodList="bloodList" class="component-right" />
    </div>

  </div>
</template>

<script setup lang="ts">
import { InvAuditStockInMaster, InvAuditStockInDetail } from '/@/api/inv/invShouGong/dto'
import { SaveStockIn, SearchStockById } from '/@/api/inv/invShouGong';
import useEditMSControls from "/@/componentsMaike/basHook/useEditMSControls";
import { isNull, showSuccess, showError, checkObjectValues } from '/@/utils/index'

const bloodStationOptions = [{ label: "本地血站", value: "本地血站" }, { label: "异地血站", value: "异地血站" }];
const courtAreaOptions = [{ label: "院本部", value: "院本部" }, { label: "广州分院", value: "广州分院" }];

const {
  master,
  resetData,
  detailList,
  // itemChange,
  // saveBill,
  isSave,
  cancelBill
} = useEditMSControls<InvAuditStockInMaster, InvAuditStockInDetail>(new InvAuditStockInMaster,
  new Array<InvAuditStockInDetail>(), SearchStockById, SaveStockIn, () => validateFun())


const divideRef = ref<HTMLElement | null>(null);
//#region 校验处理
// import { useValidateFun } from './business/validateFun'
// const { formRef, validateFun } = useValidateFun(detailList)
//#endregion
const addBill = () => {
  isSave.value = false

  bloodList.value = []
  Object.keys(master).forEach(key => {
    master[key] = undefined
  })
  if (divideRef.value && (divideRef.value as any).changeHeight) {
    (divideRef.value as any).changeHeight()
  }
}
const keyupEnter = () => {
  if (master.bloodBank) {
    bloodList.value = bloodData
  }
}
const bloodList = ref<any>([])
const bloodData = [
  {
    id: 1,
    bloodDonationCode: '440152202500001',
    basBloodProductCode: 'BPC001',
    basBloodProductName: '红细胞悬液',
    courtArea: '院区A',
    abo: 'A',
    rhD: '+',
    volume: 2,
    unit: 'u',
    price: 250,
    status: '正常',
    effectiveDate: '2025-03-15',
    gatheringDate: '2025-02-20',
    makeDate: '2025-02-20',
    bloodStation: '血站A',
    bloodAppearance: '正常'
  },
  {
    id: 2,
    bloodDonationCode: '440152202500002',
    basBloodProductCode: 'BPC002',
    basBloodProductName: '洗涤红细胞',
    courtArea: '院区B',
    abo: 'B',
    rhD: '+',
    volume: 2,
    unit: 'u',
    price: 450,
    status: '正常',
    effectiveDate: '2025-03-20',
    gatheringDate: '2025-02-21',
    makeDate: '2025-02-21',
    bloodStation: '血站B',
    bloodAppearance: '正常'
  },
  {
    id: 3,
    bloodDonationCode: '440152202500003',
    basBloodProductCode: 'BPC003',
    basBloodProductName: '新鲜冰冻血浆',
    courtArea: '院区A',
    abo: 'AB',
    rhD: '+',
    volume: 150,
    unit: 'ml',
    price: 300,
    status: '正常',
    effectiveDate: '2025-03-25',
    gatheringDate: '2025-02-22',
    makeDate: '2025-02-22',
    bloodStation: '血站A',
    bloodAppearance: '正常'
  },
  {
    id: 4,
    bloodDonationCode: '440152202500004',
    basBloodProductCode: 'BPC004',
    basBloodProductName: '机采血小板',
    courtArea: '院区C',
    abo: 'O',
    rhD: '+',
    volume: 1,
    unit: 'u',
    price: 350,
    status: '正常',
    effectiveDate: '2025-03-30',
    gatheringDate: '2025-02-23',
    makeDate: '2025-02-23',
    bloodStation: '血站C',
    bloodAppearance: '正常'
  },
  {
    id: 5,
    bloodDonationCode: '440152202500005',
    basBloodProductCode: 'BPC005',
    basBloodProductName: '冰冻血浆',
    courtArea: '院区B',
    abo: 'A',
    rhD: '+',
    volume: 50,
    unit: 'ml',
    price: 400,
    status: '正常',
    effectiveDate: '2025-03-18',
    gatheringDate: '2025-02-24',
    makeDate: '2025-02-24',
    bloodStation: '血站B',
    bloodAppearance: '正常'
  },
  {
    id: 6,
    bloodDonationCode: '440152202500006',
    basBloodProductCode: 'BPC006',
    basBloodProductName: '冷沉淀',
    courtArea: '院区A',
    abo: 'B',
    rhD: '+',
    volume: 1,
    unit: 'u',
    price: 450,
    status: '正常',
    effectiveDate: '2025-03-22',
    gatheringDate: '2025-02-25',
    makeDate: '2025-02-25',
    bloodStation: '血站A',
    bloodAppearance: '正常'
  },
  {
    id: 7,
    bloodDonationCode: '4401522025000015',
    basBloodProductCode: 'BPC013',
    basBloodProductName: '血浆',
    basBloodVarietyId: 4,
    courtArea: '院区C',
    abo: 'AB',
    rhD: '+',
    volume: 300,
    unit: 'ml',
    price: 280,
    status: '正常',
    effectiveDate: '2025-03-28',
    gatheringDate: '2025-02-26',
    makeDate: '2025-02-26',
    bloodStation: '血站C',
    bloodAppearance: '正常'
  },
  {
    id: 8,
    bloodDonationCode: '4401522025000025',
    basBloodProductCode: 'BPC015',
    basBloodProductName: '血浆',
    basBloodVarietyId: 4,
    courtArea: '院区B',
    abo: 'O',
    rhD: '-',
    volume: 350,
    unit: 'ml',
    price: 320,
    status: '正常',
    effectiveDate: '2025-03-31',
    gatheringDate: '2025-02-27',
    makeDate: '2025-02-27',
    bloodStation: '血站B',
    bloodAppearance: '正常'
  },
  {
    id: 9,
    bloodDonationCode: '440152202500022',
    basBloodProductCode: 'BPC002',
    basBloodProductName: '洗涤红细胞',
    basBloodVarietyId: 5,
    courtArea: '院区A',
    abo: 'A',
    rhD: '+',
    volume: 2,
    unit: 'u',
    price: 220,
    status: '正常',
    effectiveDate: '2025-03-12',
    gatheringDate: '2025-02-20',
    makeDate: '2025-02-20',
    bloodStation: '血站A',
    bloodAppearance: '正常'
  },
  {
    id: 10,
    bloodDonationCode: '440152202500032',
    basBloodProductCode: 'BPC002',
    basBloodProductName: '洗涤红细胞',
    basBloodVarietyId: 5,
    courtArea: '院区C',
    abo: 'B',
    rhD: '+',
    volume: 1,
    unit: 'u',
    price: 240,
    status: '正常',
    effectiveDate: '2025-03-16',
    gatheringDate: '2025-02-21',
    makeDate: '2025-02-21',
    bloodStation: '血站C',
    bloodAppearance: '正常'
  },
  {
    id: 11,
    bloodDonationCode: '440152202500011',
    basBloodProductCode: 'BPC001',
    basBloodProductName: '红细胞悬液',
    basBloodVarietyId: 1,
    courtArea: '院区B',
    abo: 'AB',
    rhD: '+',
    volume: 0.5,
    unit: 'u',
    price: 350,
    status: '正常',
    effectiveDate: '2025-03-23',
    gatheringDate: '2025-02-22',
    makeDate: '2025-02-22',
    bloodStation: '血站B',
    bloodAppearance: '正常'
  },
  {
    id: 12,
    bloodDonationCode: '440152202500012',
    basBloodProductCode: 'BPC002',
    basBloodProductName: '洗涤红细胞',
    courtArea: '院区A',
    abo: 'O',
    rhD: '-',
    volume: 1,
    unit: 'u',
    price: 480,
    status: '正常',
    effectiveDate: '2025-03-27',
    gatheringDate: '2025-02-23',
    makeDate: '2025-02-23',
    bloodStation: '血站A',
    bloodAppearance: '正常'
  },
  {
    id: 13,
    bloodDonationCode: '440152202500013',
    basBloodProductCode: 'BPC003',
    basBloodProductName: '新鲜冰冻血浆',
    courtArea: '院区C',
    abo: 'A',
    rhD: '+',
    volume: 200,
    unit: 'ml',
    price: 320,
    status: '正常',
    effectiveDate: '2025-03-30',
    gatheringDate: '2025-02-24',
    makeDate: '2025-02-24',
    bloodStation: '血站C',
    bloodAppearance: '正常'
  },
  {
    id: 14,
    bloodDonationCode: '440152202500014',
    basBloodProductCode: 'BPC004',
    basBloodProductName: '机采血小板',
    basBloodVarietyId: 2,
    courtArea: '院区B',
    abo: 'B',
    rhD: '+',
    volume: 100,
    unit: 'u',
    price: 360,
    status: '正常',
    effectiveDate: '2025-04-02',
    gatheringDate: '2025-02-25',
    makeDate: '2025-02-25',
    bloodStation: '血站B',
    bloodAppearance: '正常'
  },
  {
    id: 15,
    bloodDonationCode: '440152202500015',
    basBloodProductCode: 'BPC005',
    basBloodProductName: '冰冻血浆',
    courtArea: '院区A',
    abo: 'AB',
    rhD: '+',
    volume: 100,
    unit: 'ml',
    price: 200,
    status: '正常',
    effectiveDate: '2025-03-20',
    gatheringDate: '2025-02-26',
    makeDate: '2025-02-26',
    bloodStation: '血站A',
    bloodAppearance: '正常'
  },
  {
    id: 16,
    bloodDonationCode: '440152202500016',
    basBloodProductCode: 'BPC006',
    basBloodProductName: '冷沉淀',
    basBloodVarietyId: 3,
    courtArea: '院区C',
    abo: 'O',
    rhD: '+',
    volume: 1,
    unit: 'u',
    price: 550,
    status: '正常',
    effectiveDate: '2025-03-24',
    gatheringDate: '2025-02-27',
    makeDate: '2025-02-27',
    bloodStation: '血站C',
    bloodAppearance: '正常'
  },
  {
    id: 17,
    bloodDonationCode: '440152202500023',
    basBloodProductCode: 'BPC003',
    basBloodProductName: '新鲜冰冻血浆',
    basBloodVarietyId: 4,
    courtArea: '院区B',
    abo: 'A',
    rhD: '+',
    volume: 200,
    unit: 'ml',
    price: 260,
    status: '正常',
    effectiveDate: '2025-03-29',
    gatheringDate: '2025-02-20',
    makeDate: '2025-02-20',
    bloodStation: '血站B',
    bloodAppearance: '正常'
  },
  {
    id: 18,
    bloodDonationCode: '440152202500033',
    basBloodProductCode: 'BPC003',
    basBloodProductName: '新鲜冰冻血浆',
    basBloodVarietyId: 4,
    courtArea: '院区A',
    abo: 'B',
    rhD: '+',
    volume: 200,
    unit: 'ml',
    price: 300,
    status: '正常',
    effectiveDate: '2025-03-31',
    gatheringDate: '2025-02-21',
    makeDate: '2025-02-21',
    bloodStation: '血站A',
    bloodAppearance: '正常'
  },
  {
    id: 19,
    bloodDonationCode: '440152202500092',
    basBloodProductCode: 'BPC002',
    basBloodProductName: '洗涤红细胞',
    basBloodVarietyId: 5,
    courtArea: '院区C',
    abo: 'AB',
    rhD: '+',
    volume: 1,
    unit: 'u',
    price: 260,
    status: '正常',
    effectiveDate: '2025-03-17',
    gatheringDate: '2025-02-22',
    makeDate: '2025-02-22',
    bloodStation: '血站C',
    bloodAppearance: '正常'
  },
  {
    id: 20,
    bloodDonationCode: '440152202500082',
    basBloodProductCode: 'BPC012',
    basBloodProductName: '洗涤红细胞',
    basBloodVarietyId: 5,
    courtArea: '院区B',
    abo: 'O',
    rhD: '-',
    volume: 1,
    unit: 'u',
    price: 280,
    status: '正常',
    effectiveDate: '2025-03-21',
    gatheringDate: '2025-02-23',
    makeDate: '2025-02-23',
    bloodStation: '血站B',
    bloodAppearance: '正常'
  }]
/**
 * 查看历史记录
 */
const gotoList = () => {
  emits('goto-list')
}
const emits = defineEmits(['goto-list'])

const isScan = ref(false)
const scanBarCode = ref()

const saveBill = () => {
  if (bloodList.value.length > 0) {
    isSave.value = true
    showSuccess()
  }
  else {
    showError('请至少添加一条数据后再提交')
  }
}

const itemChange = (row: any) => {
  isSave.value = true
  bloodList.value = bloodData
  master.bloodBank = row.bloodBank
}

const scanCode = () => {

}
const delInfo = () => { }
defineExpose({
  itemChange
})

</script>
<style lang='scss' scoped>
.scanBox {
  display: flex;
  width: 100%;

  .scanInpBox {
    flex-grow: 0.05;


    :deep(.el-input__wrapper) {
      box-shadow: 0 0 0 1px #c4c4c4 inset;
      // background-color: #d6e9fd !important;
    }

    :deep(.el-input__wrapper.is-focus) {
      box-shadow: 0 0 0 2px #000 inset;
    }
  }

  .buttonBox {
    margin-left: 3px;

    .delBtn {
      :deep(.el-button) {
        background-color: #ecf5ff;
        color: #929292;
      }
    }
  }
}

.resize-container {
  display: flex;
  width: 100%;

  .component-left {
    flex: 1;
  }

  .component-right {
    width: 40%;
  }
}
</style>